package com.sample.camerax.exp

import android.annotation.SuppressLint
import android.widget.ImageButton
import androidx.appcompat.view.menu.MenuPopupHelper
import androidx.appcompat.widget.PopupMenu
import androidx.camera.core.Camera
import androidx.camera.core.ImageCapture
import androidx.camera.core.TorchState
import com.loyal.camerax.R

/** Milliseconds used for UI animations */
const val ANIMATION_FAST_MILLIS = 50L
const val ANIMATION_SLOW_MILLIS = 100L

fun ImageButton.switchFlash(imageCapture: ImageCapture?, camera: Camera?) {
    // View当前PopupMenu显示的相对View的位置
    val cameraControl = camera?.cameraControl
    val popupMenu = PopupMenu(context, this)
    val inflater = popupMenu.menuInflater
    // menu布局
    inflater.inflate(R.menu.menu_flash, popupMenu.menu)
    // menu的item点击事件
    popupMenu.setOnMenuItemClickListener {
        when (it.itemId) {
            R.id.menu_flash_on -> {
                cameraControl?.enableTorch(false)
                setImageResource(R.drawable.flash_on)
                imageCapture?.flashMode = ImageCapture.FLASH_MODE_ON
            }
            R.id.menu_flash_auto -> {
                cameraControl?.enableTorch(false)
                setImageResource(R.drawable.flash_auto)
                imageCapture?.flashMode = ImageCapture.FLASH_MODE_AUTO
            }
            R.id.menu_flash_torch -> {
                cameraControl?.enableTorch(true)
                setImageResource(R.drawable.flash_torch)
            }
            R.id.menu_flash_off -> {
                cameraControl?.enableTorch(false)
                setImageResource(R.drawable.flash_off)
                imageCapture?.flashMode = ImageCapture.FLASH_MODE_OFF
            }
        }
        false
    }
    showMenuIcon(popupMenu)
    popupMenu.show()
}

fun ImageButton.setFlashIcon(imageCapture: ImageCapture?, camera: Camera?) {
    // View当前PopupMenu显示的相对View的位置
    val cameraControl = camera?.cameraControl
    val cameraInfo = camera?.cameraInfo
    val flashMode = imageCapture?.flashMode
    when {
        ImageCapture.FLASH_MODE_ON == flashMode -> {
            setImageResource(R.drawable.flash_on)
        }
        ImageCapture.FLASH_MODE_AUTO == flashMode -> {
            setImageResource(R.drawable.flash_auto)
        }
        ImageCapture.FLASH_MODE_OFF == flashMode -> {
            setImageResource(R.drawable.flash_off)
        }
    }
    if (TorchState.ON == cameraInfo?.torchState?.value) {
        cameraControl?.enableTorch(true)
        setImageResource(R.drawable.flash_torch)
    } else {
        cameraControl?.enableTorch(false)
        setImageResource(R.drawable.flash_off)
        imageCapture?.flashMode = ImageCapture.FLASH_MODE_OFF
    }
}

/**
 * 使用反射。强制显示菜单图标
 */
@SuppressLint("RestrictedApi")
private fun showMenuIcon(popupMenu: PopupMenu) {
    try {
        val field = popupMenu.javaClass.getDeclaredField("mPopup")
        field.isAccessible = true
        val helper = field[popupMenu] as MenuPopupHelper
        helper.setForceShowIcon(true)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}